package com.xzs.kit.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description :
 * @author : Victor.Xiao
 * @date : 2017/12/8 10:55
 */
public class CommUtil {

	static Logger logger =LoggerFactory.getLogger(CommUtil.class);
	
	public static String nullString(Object s) {
		return ((s == null) ? null : s.toString());
	}

	public static String null2String(Object s) {
		return ((s == null) ? "" : s.toString());
	}

	public static String null2String(String s) {
		return ((s == null) ? "" : s.toString());
	}
	
    public static String null2String(String s, String defaultStr) {
        return ((s == null) ? defaultStr : s.toString());
    }
    
    public static Boolean null2Boolean(String s, Boolean defaultBoolean) {
        return ((s == null) ? defaultBoolean : Boolean.parseBoolean(s));
    }

	public static int null2Int(Object s) {
		int v = 0;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static int null2Int(String s) {
		int v = 0;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static int null2Int(String s, int defaultValue) {
		int v = defaultValue;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static int null2Int(Object s, int defaultValue) {
		int v = defaultValue;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Integer.parseInt(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static long null2Long(String s) {
		long v = 0L;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Long.parseLong(s);
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static long null2Long(Object s) {
		long v = 0L;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Long.parseLong(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}
	
	public static long null2Long(Object s, long defaultValue) {
		long v = defaultValue;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Long.parseLong(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static double null2Double(Object s, int defaultValue) {
		double v = defaultValue;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Double.parseDouble(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}
	
	public static double null2Double(Object s) {
		double v = 0.0D;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Double.parseDouble(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}

	public static float null2Float(Object s, int defaultValue) {
		float v = defaultValue;
		if ((s != null) && (!(s.equals("")))) {
			try {
				v = Float.parseFloat(s.toString());
			} catch (Exception e) {
				logger.error("将数据进行Null处理时出错");
			}
		}
		return v;
	}
	
	public static int double2Int(double d) {
		int returnValue = 0;
		returnValue = (int) d;
		return returnValue;
	}

	public static String array2String(String[] values, String split) {
		StringBuffer returnValue = new StringBuffer();
		if (values != null) {
			int length = values.length;
			for (int i = 0; i < length; ++i) {
				returnValue.append(values[i].concat(split));
			}
		}
		return returnValue.toString();
	}

	public static String array2String(int[] values, String split) {
		StringBuffer returnValue = new StringBuffer();
		if (values != null) {
			int length = values.length;
			for (int i = 0; i < length; ++i) {
				returnValue.append(String.valueOf(values[i]).concat(split));
			}
		}
		return returnValue.toString();
	}

	public static int[] arrayString2Int(String[] values) {
		int[] returnValue = (int[]) null;
		if (values != null) {
			int length = values.length;
			returnValue = new int[length];
			for (int i = 0; i < length; ++i) {
				returnValue[i] = Integer.parseInt(values[i]);
			}
		}
		return returnValue;
	}

	public static long[] arrayString2Long(String[] values) {
		long[] returnValue = (long[]) null;
		if (values != null) {
			int length = values.length;
			returnValue = new long[length];
			for (int i = 0; i < length; ++i) {
				returnValue[i] = Long.parseLong(values[i]);
			}
		}
		return returnValue;
	}

	public static double[] arrayString2Double(String[] values) {
		double[] returnValue = (double[]) null;
		if (values != null) {
			int length = values.length;
			returnValue = new double[length];
			for (int i = 0; i < length; ++i) {
				returnValue[i] = Double.parseDouble(values[i]);
			}
		}
		return returnValue;
	}
	
	/**
	 * StringArray转化为String
	 */
	public static String arrayString2String(String[] values, String split) {
		String returnValue = null;
		if (values != null) {
			int length = values.length;
			for (int i = 0; i < length; i++) {
				returnValue += values[i] + split;
			}
			if(!returnValue.equals("") && returnValue.length() > 0){
				returnValue = returnValue.substring(returnValue.length() - split.length(), returnValue.length());
			}
		}
		return ((returnValue == null) ? "" : returnValue.toString());
	}
	
	/**
	 * String转化为StringArray
	 */
	public static String[] string2ArrayString(String values, String split) {
		String returnValue[] = null;
		if (values != null) {
			returnValue = values.split(split);
		}
		return ((returnValue == null) ? new String[0] : returnValue);
	}
	
	public static String changeIdToString(String value){
		if(value.indexOf(",")>-1){
			value = "'"+value.replaceAll(",", "','")+"'";
		}
		return value;
	}
	
	/**
	 * 判断变量是否为空
	 * 
	 * @param s
	 * @return
	 */
	public static boolean isEmpty(String s) {
		if (null == s || "".equals(s) || "".equals(s.trim()) || "null".equalsIgnoreCase(s)) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 用来去掉List中空值和相同项的。
	 * 
	 * @param list
	 * @return
	 */
	public static List<String> removeSameItem(List<String> list) {
		List<String> difList = new ArrayList<String>();
		for (String t : list) {
			if (t != null && !difList.contains(t)) {
				difList.add(t);
			}
		}
		return difList;
	}
	
	
	/**
	 * 获取当前时间
	 * @return
	 */
    public static String getNowTime(){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
        Date date = new Date();  
        String tim = sdf.format(date);  
      	return tim;  
    }
    
    public static String padding(String str,String paddingStr,int len){
    	for(int i =0;i<len;i++){
    		str+=paddingStr;
    	}
    	return str;
    }
    
    public static String getUUID(){
    	UUID uuid = UUID.randomUUID();
    	String uuidStr = uuid.toString().replaceAll("-", "");
    	return uuidStr; 
    }
    
    
    /**
     * set **** to **:**
     * @return
     */
    public static String formatTime(String time){
    	int length = time.length();
    	if(length<4){
    		for(int i=0;i<(4-length);i++){
    			time = "0" + time;
    		}
    	}
    	time = time.substring(0, 2)+":"+time.substring(2, 4);
    	return time;
    }
    
    public static int getIntervalDays(Date fDate, Date oDate) {

        if (null == fDate || null == oDate) {

            return -1;

        }

        long intervalMilli = oDate.getTime() - fDate.getTime();
        
        if(intervalMilli < 0)
        {
        	return 0;
        }

        return (int) (intervalMilli / (24 * 60 * 60 * 1000) + 1);

     }
    
	public static String getRandomString(int length, int type) { //length表示生成字符串的长度  
	    String base0 = "0123456789";
	    String base1 = "abcdefghijklmnopqrstuvwxyz";
	    String base2 = "abcdefghijklmnopqrstuvwxyz0123456789";
	    String base = "";
	    if(type == 0)
	    {
	    	base = base0;
	    }
	    if(type == 1)
	    {
	    	base = base1;
	    }
	    if(type == 2)
	    {
	    	base = base2;
	    }
	    Random random = new Random();     
	    StringBuffer sb = new StringBuffer();     
	    for (int i = 0; i < length; i++) {     
	        int number = random.nextInt(base.length());     
	        sb.append(base.charAt(number));     
	    }     
	    return sb.toString();     
	 }

    public static void removeNullValue(Map<String,Object> map){
    	if(null == map){
    		return;
		}
		Set<String> set = map.keySet();
    	if(set.size()<1){
    		return;
		}
		for(Iterator<String> iterable = set.iterator();iterable.hasNext();){
			String key = iterable.next();
			if(map.get(key)==null){
				iterable.remove();
			}
		}
	}
}
